package com.epoint.androidphone.mobileoa.ui.mail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.androidmobile.bizlogic.mail.model.MailInfoModel;
import com.epoint.androidmobile.bizlogic.mail.task.Mail_GetList_Task;
import com.epoint.androidmobile.core.control.AlertUtil;
import com.epoint.androidmobile.core.string.StringHelp;
import com.epoint.androidmobile.core.xml.XMLUtil;
import com.epoint.androidphone.R;
import com.epoint.androidphone.mobileoa.ui.superview.SuperPhonePage;

/**
 * @author liyc
 * @time 2013-5-6 下午5:02:53
 * @annomation
 */
public class MailBoxListView extends SuperPhonePage {

	long getMailListTaskId;

	int index = 1, pagesize = 20;
	ListView lv;
	List<MailInfoModel> inboxlist = new ArrayList<MailInfoModel>();
	LVAdapter adapter = new LVAdapter();
	EditText etCondition;
	ImageView ivQuery;
	String boxType;
	/**
	 * 任务邮件状态
	 */
	String NeedTaskMonitor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContentViewAndTitle(R.layout.mailinbox_ws, getIntentViewTitle());
		lv = (ListView) findViewById(R.id.lv);
		lv.setOnItemClickListener(this);
		etCondition = (EditText) findViewById(R.id.etCondition);
		ivQuery = (ImageView) findViewById(R.id.ivQuery);
		ivQuery.setOnClickListener(this);
		addFoot();

		boxType = getIntent().getStringExtra("boxType");
		NeedTaskMonitor = getIntent().getStringExtra("NeedTaskMonitor");

		if (null != NeedTaskMonitor) {
			getBtRight().setText("操作");
			getBtRight().setVisibility(View.VISIBLE);
		}
		// 收件箱
		lv.setAdapter(adapter);
		getData(index, pagesize, "", boxType);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v == ivQuery) {
			inboxlist.clear();
			index = 1;
			if (lv.getFooterViewsCount() == 0) {
				addFoot();
			}
			lv.setVisibility(View.INVISIBLE);
			getData(index, pagesize, etCondition.getText().toString(), boxType);
		} else if (v == getBtRight()) {
			AlertUtil.showAlertMenu(this, "查询",
					new String[] { "已结束任务", "待办任务" },
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							if (which == 0) {
								NeedTaskMonitor = Mail_GetList_Task.TASK_DONE_TYPE;
							} else if (which == 1) {
								NeedTaskMonitor = Mail_GetList_Task.TASK_TODO_TYPE;
							}
							inboxlist.clear();
							index = 1;
							getData(index, pagesize, "", boxType);
						}
					});
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO 自动生成的方法存根
		super.onItemClick(parent, view, position, id);
		if (position == inboxlist.size()) {
			index++;
			getData(index, pagesize, etCondition.getText().toString(), boxType);
			return;
		} else {
			MailInfoModel m = inboxlist.get(position);
			Intent intent = new Intent(this, MailInBoxDetailView.class);
			intent.putExtra("mailguid", m.MAILGUID);
			intent.putExtra("boxType", boxType);
			startActivity(intent);
		}
	}

	private void getData(int _index, int _pagesize, String _keyWord,
			String _boxType) {
		showTopProgressBar();
		HashMap<String, Object> param = getPassMap();
		param.put("boxType", _boxType);
		param.put("KeyWord", _keyWord);
		param.put("CurrentPageIndex", _index + "");
		param.put("PageSize", _pagesize + "");
		param.put("NeedTaskMonitor", NeedTaskMonitor);
		getMailListTaskId = new Mail_GetList_Task(this, param).startTask();
	}

	private LinearLayout foot;

	public void addFoot() {
		TextView load_bt = new TextView(this);
		load_bt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
		load_bt.setText("更多...");
		load_bt.setGravity(Gravity.CENTER);
		foot = new LinearLayout(this);
		foot.addView(load_bt, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT, 55));
		foot.setGravity(Gravity.CENTER);
		lv.addFooterView(foot);
		registerForContextMenu(lv);
	}

	@Override
	public void refreshUI(long taskId, Object obj) {
		super.refreshUI(taskId, obj);
		if (taskId == getMailListTaskId) {
			lv.setVisibility(View.VISIBLE);
			if (validateXML(obj)) {

				String MailListXml = StringHelp.getXMLAttOut(obj.toString(),
						"MailList");
				List<MailInfoModel> listtemp = XMLUtil.DomAnalysisCommon(
						MailListXml, MailInfoModel.class);

				if (getString(R.string.WebServiceType).equals("2")
						&& (Mail_GetList_Task.INBOX_TYPE.equals(boxType) || Mail_GetList_Task.OUTBOX_TYPE
								.equals(boxType))
						|| Mail_GetList_Task.DRAFTS_TYPE.equals(boxType)
						|| Mail_GetList_Task.RECYCLE_TYPE.equals(boxType)) {
					listtemp.remove(listtemp.size() - 1);
				}

				for (MailInfoModel a : listtemp) {
					inboxlist.add(a);
				}
				adapter.notifyDataSetChanged();

				if (listtemp.size() < pagesize) {
					lv.removeFooterView(foot);
				}
				adapter.notifyDataSetChanged();
			}
		}
	}

	class LVAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO 自动生成的方法存根
			return inboxlist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO 自动生成的方法存根
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO 自动生成的方法存根
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			LayoutInflater mInflater = LayoutInflater.from(getContext());
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.maillist_ws, null);
				holder.tvTitle = (TextView) convertView
						.findViewById(R.id.tvTitle);
				holder.tvSender = (TextView) convertView
						.findViewById(R.id.tvSender);
				holder.tvTime = (TextView) convertView
						.findViewById(R.id.tvTime);
				holder.ivStatus = (ImageView) convertView
						.findViewById(R.id.ivStatus);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			MailInfoModel a = inboxlist.get(position);

			if (Mail_GetList_Task.INBOX_TYPE.equals(boxType)
					|| Mail_GetList_Task.TASK_INBOX_TYPE.equals(boxType)) {
				if ("1".equals(a.haveread)) {
					holder.ivStatus.setVisibility(View.GONE);
				} else {
					holder.ivStatus.setVisibility(View.VISIBLE);
				}
			} else if (Mail_GetList_Task.OUTBOX_TYPE.equals(boxType)
					|| Mail_GetList_Task.TASK_OUTBOX_TYPE.equals(boxType)) {
				if ("1".equals(a.IsAllSign)) {
					holder.ivStatus.setVisibility(View.GONE);
				} else {
					holder.ivStatus.setVisibility(View.VISIBLE);
				}
			}
			holder.tvTitle.setText(a.Subject);
			holder.tvSender.setText(a.FROMDISPNAME);
			holder.tvTime.setText(a.SENDTIME);

			return convertView;
		}

	}

	class ViewHolder {
		public ImageView ivStatus;
		public TextView tvTitle;
		public TextView tvSender;
		public TextView tvTime;
	}
}
